package com.guiji.quartz.domain;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.task.DataSaveUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description: 不需要代理
 * @author: wangxw
 * @date 2021/11/29 14:56
 */
public class DeadspinFootball {
	private static final Logger log = LoggerFactory.getLogger(DeadspinFootball.class);

	public static void mainMethod() throws Exception {
		HttpResponse response = null;
		try {
			response = HttpRequest.get("https://deadspin.com/football")
				.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
				.header("Accept-Encoding", "gzip, deflate")
				.setConnectionTimeout(30000)
				.execute();

		} catch (Exception e) {
			log.error("访问 https://deadspin.com/football 失败" + e);
			return;
		}
		if (null != response && response.getStatus() == 200) {
			List<HttpCookie> cookies = response.getCookies();
			String resp = response.body();
			Set<String> resUrl = getUrl(resp);
			getContent(resUrl, cookies);
		} else {
			log.error("https://deadspin.com/football " + String.valueOf(response.getStatus()));
		}

	}

	private static void getContent(Set<String> listContent, List<HttpCookie> cookies) throws MalformedURLException, URISyntaxException, InterruptedException {
		for (String url : listContent) {
			Thread.sleep(1000);
			if (!url.startsWith("https")) {
				url = new StringBuffer("https://deadspin.com").append(url).toString();
			}
			if (null != cookies && cookies.size() > 0) {
				CookieManager ccm = new CookieManager();
				CookieStore CookieStore = ccm.getCookieStore();
				for (HttpCookie cookie : cookies) {
					URL domain = new URL("https://deadspin.com");
					CookieStore.add(domain.toURI(), cookie);
				}
				HttpRequest.setCookieManager(ccm);
			}

			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc;
			JXDocument jxDocument;
			try {
				HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
				doc = response.body();
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("代理失败2" + e);
				continue;
			}
			String title = null;
			try {
				String patternTitle = "<h1(.*?)>(.*?)</h1>";
				Pattern pTitle = Pattern.compile(patternTitle);
				Matcher mTitle = pTitle.matcher(doc);
				if (mTitle.find()) {
					title = mTitle.group(2);
				}

			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			String contentStr = null;
			String mainDoc = null;
			JXDocument jxDocumentP;
			Document document = null;
			try {
				if (!doc.contains("</main>")) {
					log.error("url:" + url + "可能网页改版了");
					continue;
				}
				int start = doc.indexOf("<main");
				int end = doc.indexOf("</main>");

				mainDoc = doc.substring(start, end + 7);
				String patternContent = "\"articleBody\":\"(.*?)\"";
				Pattern pContent = Pattern.compile(patternContent);
				Matcher mContent = pContent.matcher(mainDoc);
				if (mContent.find()) {
					contentStr = mContent.group(1);
				}
				if (null != contentStr) {
					contentStr = contentStr.replace("\\n\\n", "$$$");
					contentStr = contentStr.replace("\\n", "$$$");
				}

			} catch (Exception e) {
				continue;
			} finally {
				if (contentStr == null) {
					continue;
				}
			}

			Set<String> newImgs = new HashSet<>();
			String img = null;
			try {
				String temp = null;
				String patternImg = "\"image\":(.*?),\"articleBody\":\"";
				Pattern pImg = Pattern.compile(patternImg);
				Matcher mImg = pImg.matcher(mainDoc);
				if (mImg.find()) {
					temp = mImg.group(1);
				}

				String patternImg2 = "\"url\":\"(.*?)\"";
				Pattern pImg2 = Pattern.compile(patternImg2);
				Matcher mImg2 = pImg2.matcher(temp);
				if (mImg2.find()) {
					img = mImg2.group(1);
					newImgs.add(img);
				}


			} catch (Exception e) {
				log.error("可能没有图片");
				img = "";
			}

			Date date = new Date();
			if (contentStr.length() > 50000 || contentStr.replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				System.out.println("=======================================================");
				System.out.println(url);
				System.out.println(title);

				for (String imgitem : newImgs) {
					System.out.println(" " + imgitem);
				}
				System.out.println(contentStr);
				System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
				DataSaveUtil.saveData(title, contentStr, newImgs, url, date, "https://deadspin.com/football", "Deadspin-Football");
				log.info(Thread.currentThread().getName() + "-----------------Deadspin-Football----------------");
			} catch (Exception e) {
				continue;
			}

		}
	}

	private static Set<String> getUrl(String content) {
		Set<String> res = new HashSet<>();
		if (!content.contains("</main>")) {
			return res;
		}
		int start = content.indexOf("<main");
		int end = content.indexOf("</main>");
		content = content.substring(start, end + 7);

		Document document = Jsoup.parse(content);
		Elements aElements = document.select("a");
		String url = null;
		if (null != aElements && aElements.size() > 0) {
			for (Element adom : aElements) {
				url = adom.attr("href");
				if (url != null && !url.isEmpty() && url.startsWith("https://deadspin.com/")) {
					if (!url.equals("https://deadspin.com/football/ncaa") && !url.equals("https://deadspin.com/football/nfl")) {
						res.add(url);
					}

				}
			}

		}
		return res;
	}


//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}

}


